/*
 * Bullet.cpp
 *
 *  Created on: 31/10/2011
 */

#include "Bullet.h"
#include "../../common/lib/SDL_rotozoom.h"

Bullet::Bullet() {}

Bullet::Bullet(int posX, int posY, Widget *parent, double angle, int duration) :
	WidgetMovimiento(posX, posY, parent) {

	surfaceReference = loadSurface(BULLET_DEFAULT_IMAGE);
	parcel = getPosition();

	std::stringstream ss;
	ss << "x = " << parcel.posX << " y = " << parcel.posY;

	Logger::log("bullet", "constructor", &ss);

	parcel.height = surfaceReference->h;
	parcel.width = surfaceReference->w;

	this->velocity = BULLET_DEFAULT_VELOCITY;
	this->duration = duration;
	this->timeAlive = 0;
	this->angle = angle;
	this->drawable = true;

	//setPosition();
	nextX = parcel.posX;
	nextY = parcel.posY;

}

void Bullet::handleEvent(Event event) {
	//No hace nada por ahora.. capaz màs adelante con las colisiones
}

void Bullet::resolve() {

	if (drawable) {
		if (!timer.is_started())
			timer.start();

		float secs = timer.get_ticks() / 1000.0;
		if (secs <= this->duration) {
			int avanceX, avanceY;

			float angleInRadians = angle * (M_PI / 180);
			avanceY = (-1.0) * velocity * sin(angleInRadians);
			avanceX = velocity * cos(angleInRadians);

			parcel.posX += avanceX;
			parcel.posY += avanceY;

			timeAlive++;
		} else {
			this->drawable = false;
			timeAlive = 0;
			timer.stop();
		}
	}

}

void Bullet::showOn(SDL_Surface* surface) {
	if (drawable) {
		surfaceShown = rotozoomSurface(surfaceReference, angle, 1.,	SMOOTHING_OFF);

		//Surface Reflection
		surfaceReflection(surfaceShown, surface);

		// Position where the surface will be draw
		SDL_Rect posRect = { parcel.posX, parcel.posY, surfaceShown->w,	surfaceShown->h };

		// Position fixed to the center of rotation
		posRect.x -= (surfaceShown->w) / 2;
		posRect.y -= (surfaceShown->h) / 2;

		//Show the original dot
		SDL_BlitSurface(surfaceShown, NULL, surface, &posRect);
		SDL_FreeSurface(surfaceShown);
	}
}

bool Bullet::isDrawable() {
	return drawable;
}

Bullet::~Bullet() {
	SDL_FreeSurface(surfaceReference);
}

void Bullet::setPosition() {
	int angulo = (int) angle;

	if ((angulo >= 0) && (angulo <= 40)) {
		parcel.posX = parcel.posX + 15;
		parcel.posY = parcel.posY + (-angulo) * 3 / 10;

		std::stringstream ss;
		ss << "x = " << parcel.posX << " y = " << parcel.posY;

		Logger::log("bullet", "setPosition", &ss);
	}

	if ((angulo > 40) && (angulo <= 130)) {
		parcel.posX = parcel.posX + (-(angulo - 90)) * 3 / 10;
		parcel.posY = parcel.posY - 15;

		std::stringstream ss;
		ss << "x = " << parcel.posX << " y = " << parcel.posY;

		Logger::log("bullet", "setPosition", &ss);
	}

	if ((angulo > 130) && (angulo <= 180)) {
		parcel.posX = parcel.posX - 15;
		parcel.posY = parcel.posY + (-(angulo - 180)) * 3 / 10;

		std::stringstream ss;
		ss << "x = " << parcel.posX << " y = " << parcel.posY;

		Logger::log("bullet", "setPosition", &ss);
	}

	if ((angulo > 180) && (angulo <= 220)) {
		parcel.posX = parcel.posX - 15;
		parcel.posY = parcel.posY + (angulo - 180) * 3 / 10;

		std::stringstream ss;
		ss << "x = " << parcel.posX << " y = " << parcel.posY;

		Logger::log("bullet", "setPosition", &ss);
	}

	if ((angulo > 220) && (angulo <= 310)) {
		parcel.posX = parcel.posX + (angulo - 270) * 3 / 10;
		parcel.posY = parcel.posY + 15;

		std::stringstream ss;
		ss << "x = " << parcel.posX << " y = " << parcel.posY;

		Logger::log("bullet", "setPosition", &ss);
	}

	if ((angulo > 310) && (angulo <= 360)) {
		parcel.posX = parcel.posX + 15;
		parcel.posY = parcel.posY + (-(angulo - 360)) * 3 / 10;

		std::stringstream ss;
		ss << "x = " << parcel.posX << " y = " << parcel.posY;

		Logger::log("bullet", "setPosition", &ss);
	}
}
